<?php namespace App\Client\Goods;

//基础条件
use App\Core\Repository\Criterias\PaginateCriteria;
use App\Core\Repository\Criterias\SortCriteria;
//过滤器
use App\Core\Repository\Criterias\Filters\InputCriteria;
use App\Core\Repository\Criterias\Filters\InputKeywordsCriteria;
use App\Core\Repository\Criterias\Filters\InputTimeRangeCriteria;

use App\Admin\Goods\Goods\Meta;
use App\Admin\Goods\Goods\Repository;

trait Service {
    
    //基础服务
    use \App\Core\Service\Service;
    use \App\Core\Service\Services\All;
    use \App\Core\Service\Services\Indexer;
    use \App\Core\Service\Services\Creater;
    use \App\Core\Service\Services\Updater;
    use \App\Core\Service\Services\Changer;
    use \App\Core\Service\Services\Deleter;
    use \App\Core\Service\Services\Detailer;
    //操作日志
    use \App\Admin\Base\LogService;
    
    protected function bootService(){
        $this->bootBaseService();
        $this->setRepository(new Repository);
        $this->setMeta(new Meta);
        $this->repository = $this->getRepository();
        //添加观察者
        $this->addListeners([
            'create' => new Listeners\CreateListener()
        ]);
    }
	
    //列表
    public function lists(){
        //过滤器
        $conditions = [
            new InputKeywordsCriteria('name,classify'),
            new InputTimeRangeCriteria('created_at'),
            new InputCriteria('is_experience'),
            new InputCriteria('status'),
            new Criterias\MatchClient()
        ];
        $count = $this->repository->addCriterias($conditions)->count();
        $conditions[] = new PaginateCriteria();
        $conditions[] = new SortCriteria('id', 'DESC');
        $data = $this->repository->resetQuery()->resetCriteria()->addCriterias($conditions)->all();
        $data->load('brand');
        return $this->success([
            'count' => $count,
            'data' => $data
        ]);
    }

    //上线
    public function online(){
        return $this->change('status', '1', '0');
    }

    //下线
    public function offline(){
        return $this->change('status', '0', '1');
    }

    //批量上线
    public function multiOnline(){
        return $this->multiChange('status', '1', '0');
    }

    //批量下线
    public function multiOffline(){
        return $this->multiChange('status', '0', '1');
    }
}